//package com.ruoyi.log.aspectj;
//
//import com.ruoyi.common.annotation.Log;
//import com.ruoyi.common.constant.Constants;
//import com.ruoyi.common.core.domain.entity.SysUser;
//import com.ruoyi.common.core.domain.model.LoginUser;
//import com.ruoyi.common.exception.BusinessException;
//import com.ruoyi.common.utils.ClassUtil;
//import com.ruoyi.common.utils.Func;
//import com.ruoyi.common.utils.ServletUtils;
//import com.ruoyi.common.utils.ip.IpUtils;
//import com.ruoyi.framework.web.service.TokenService;
//import com.ruoyi.log.domain.MangoLog;
//import com.ruoyi.log.service.LogService;
//import lombok.AllArgsConstructor;
//import org.aspectj.lang.JoinPoint;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Async;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.http.HttpServletRequest;
//import java.lang.reflect.Method;
//import java.time.LocalDateTime;
//
//
//@Component
//@AllArgsConstructor
//public class LogAsync {
//
//    private LogService logService;
//
//    private TokenService tokenService;
//
//    @Async("threadPoolTaskExecutor")
//    public void saveELog(JoinPoint joinPoint, Exception e) {
//        MethodSignature ms = (MethodSignature) joinPoint.getSignature();
//        Method method = ms.getMethod();
//        //反射类获取参数
//        Log annotation = ClassUtil.getAnnotation(method, Log.class);
//        MangoLog mangoLog = getMangoLog(e);
//        mangoLog.setType(annotation.title());
//        Object[] args = joinPoint.getArgs();
//        if (Func.isNotEmpty(args)){
//            mangoLog.setRequestParams(args[0].toString());
//            mangoLog.setCreateDate(LocalDateTime.now());
//        }
//        logService.save(mangoLog, BizConstants.MANGO_EXCEPTION_LOG);
//    }
//
//
//    @Async("threadPoolTaskExecutor")
//    public void saveELog(String title,Exception e) {
//        MangoLog mangoLog = getMangoLog(e);
//        mangoLog.setType(title);
//        logService.save(mangoLog, BizConstants.MANGO_EXCEPTION_LOG);
//    }
//
//    @Async("threadPoolTaskExecutor")
//    public void info(String type,String message) {
//        MangoLog mangoLog = new MangoLog();
//        mangoLog.setType(type);
//        mangoLog.setMessage(message);
//        logService.save(mangoLog, BizConstants.INFO_LOG);
//    }
//
//
//    private MangoLog getMangoLog(Exception e){
//        MangoLog mangoLog = new MangoLog();
//        //HttpServletRequest request = ServletUtils.getRequest();
//        //LoginUser loginUser = tokenService.getLoginUser(request);
////        if (Func.isNotEmpty(loginUser) && Func.isNotEmpty(loginUser.getUser())){
////            SysUser user = loginUser.getUser();
////            mangoLog.setUsername(user.getUserName());
////        }
////        mangoLog.setIp(IpUtils.getIpAddr(request));
////        mangoLog.setRequestUri(request.getRequestURI());
//        //异常信息
//        if (e instanceof BusinessException){
//            BusinessException businessException = (BusinessException)e;
//            mangoLog.setCode(businessException.getCode()+"");
//            mangoLog.setDetail(businessException.getDetails());
//        }
//        StackTraceElement[] stackTrace = e.getStackTrace();
//        StringBuilder stackTraceStrs = new StringBuilder();
//        for (int i = 0; i < (stackTrace.length > 10 ? 10 : stackTrace.length); i++) {
//            stackTraceStrs.append(stackTrace[i].toString()).append("\n");
//        }
//        mangoLog.setStackTrance(stackTraceStrs.toString());
//
//        mangoLog.setExceptionName(e.getClass().getName());
//        mangoLog.setMessage(e.getMessage());
//        mangoLog.setCreateDate(LocalDateTime.now());
//        return mangoLog;
//    }
//
//
//}
